// pages/game2/game2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    success:false,
    num: 0,
    correctnum:0,//做对的题数
    correctrate:0,//正确率
    tihao: Math.round(Math.random()*41),
    dataList:[
      '醉卧古藤阴下，了不知南北',
      '孤舟蓑笠翁，独钓寒江雪',
      '但使主人能醉客，不知何处是他乡',
      '墙角数枝梅，凌寒独自开',
      '碧玉妆成一树高，万条垂下绿丝绦',
      '营州少年厌原野，狐裘蒙茸猎城下',
      '百亩庭中半是苔，桃花净尽菜花开',
    ],
    list:['A','B','C','D'],
    
    btnArr:[]
  },

  
  onReady: function () {
    this.getData()
  },

  getData(){
    let arr=[];
    let filterArr = JSON.parse(JSON.stringify(this.data.dataList));
    arr.push(this.data.dataList[this.data.tihao])
    // splice(a,b)a开始删除b个，用完删除，排重
    filterArr.splice(this.data.tihao, 1) 
   
    filterArr.sort(function () {
      return .5 - Math.random();
    }).sort(function () {
      return .5 - Math.random();
      })
    arr = [...arr, ...filterArr.splice(0, 3)].sort(function () {
      return .5 - Math.random();
    })
    this.setData({
      btnArr:arr
    })
  },
  back() {
    wx.navigateBack({
      delta: 1
    })
  },
  push(event){
      console.log(event.target.dataset.str)
      console.log(this.data.dataList[this.data.tihao])
      console.log(this.data.tihao)
      if (event.target.dataset.str == this.data.dataList[this.data.tihao]){
        console.log('对了')
        var that = this
        wx.showModal({
          title: '提示',
          content: '回答正确',
          confirmText: "下一题",
          success(res) {
            if (res.confirm) {
              if (that.data.num + 1 == that.data.dataList.length) {
                that.setData({
                  correctrate: (((that.data.correctnum + 1) / (that.data.num + 1)) * 100).toFixed(2),
                  success: true
                })
                return
              }
              that.setData({
                num: that.data.num + 1,
                correctnum: that.data.correctnum + 1,
                tihao: Math.round(Math.random()*6)
              })
              that.getData()
            } else if (res.cancel) {
            }
          }
        })
      }else{
        var that = this
        wx.showModal({
          title: '提示',
          content: '回答错误',
          confirmText: "下一题",
          success(res) {
              if (res.confirm) {
                  if (that.data.num + 1 == that.data.dataList.length) {
                    that.setData({
                      correctrate: (((that.data.correctnum) / (that.data.num + 1)) * 100).toFixed(2),
                      success: true
                    })
                    return
                  }
                  that.setData({
                    num: that.data.num + 1,
                    tihao: Math.round(Math.random()*6)
                  })
                  that.getData()
                } else if (res.cancel) {
                }
          }
        })
      }
    }
})